package all.com.jinyongqunxiax.model.mod;

import com.randy.fundation.JxAssert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import all.com.jinyongqunxiax.BuildConfig;
import androidx.annotation.NonNull;
import jy.game.components.resource.JyResource;
import jy.game.tools.BasePojoManager;

public class ResourceManager {

    public List<JyResource>        list = new ArrayList<>();
    public Map<String, JyResource> map  = new HashMap<>();


    public Map<String, JyResource> resourcesByValue = new HashMap<>();
    public Map<String, JyResource> resourcesByKey   = map;

    String xmlPath;

    public  ResourceManager(@NonNull String xmlPath) {
        this.xmlPath = xmlPath;
    }

    BasePojoManager<JyResource> manager;

    public String load() {
        map.clear();
        list.clear();

        manager = BasePojoManager.build(xmlPath, JyResource.class);
        list = manager.datas;
        if (!manager.success()) {
            return manager.message;
        }
        StringBuilder message = new StringBuilder(getClass().getSimpleName());

        for (JyResource item : list) {
            if (map.containsKey(item.key)) {
                message.append("发现重复:").append(item.key);
                continue;
            }
            map.put(item.key, item);
        }

        for (JyResource item : list) {
            if (resourcesByValue.containsKey(item.value)) {
                message.append("发现重复:").append(item.value);
                continue;
            }
            resourcesByValue.put(item.value, item);
        }


        return manager.message;
    }
//    String load() {
//        // 解析mod参数
//        try {
//            File                   file           = new File(xmlPath);
//            FileInputStream        stream         = new FileInputStream(file);
//            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
//            DocumentBuilder        docBuilder     = builderFactory.newDocumentBuilder();
//            Document               doc            = docBuilder.parse(stream);
//            NodeList               rootList       = doc.getElementsByTagName("root");
//
//            Node rootNode = null;
//            if (rootList.getLength() == 1) {
//                rootNode = rootList.item(0);
//            }
//            if (rootNode == null) {
//                Logger.e("bad format %s", xmlPath);
//                return xmlPath + "bad format";
//            }
//
//            NodeList children      = rootNode.getChildNodes();
//            int      numberOfChild = children.getLength();
//            for (int i = 0; i < numberOfChild; i++) {
//                // <resource icon="" key="头像.尼姑" value="Heads/nigu"/>
//                Node dataNode = children.item(i);
//                int  nodeType = dataNode.getNodeType();
//                if (nodeType != Node.ELEMENT_NODE) {
//                    continue;
//                }
//                NamedNodeMap attributes = dataNode.getAttributes();
//                if (attributes != null) {
//                    String key   = XmlUtils.attr(attributes, "key");
//                    String value = XmlUtils.attr(attributes, "value");
//
//                    resourcesByValue.put(value, new Resource(key, value));
//                    resourcesByKey.put(key, new Resource(key, value));
//
//                }
//            }
//        } catch (Exception e) {
//            return e.getMessage();
//        }
//        return "";
//    }
//
//    /**
//     * Scripts/resource.xml
//     */
//    public static class Resource {
//        public String key;
//        String value;
//
//        Resource(String key, String value) {
//            this.key = key;
//            this.value = value;
//        }
//    }
}
